﻿using System;
using System.Collections.Generic;
using EasyPhysic.Collider;

namespace EasyPhysic.Manager
{
    public partial  class PhysicSystem
    {
        class  ReferencePool
        {
            
            static Dictionary<Type,Queue<ColliderWrapper>> _pools = new Dictionary<Type, Queue<ColliderWrapper>>();
            /// <summary>
            /// 释放一个对象
            /// </summary>
            /// <param name="wrapper"></param>
            public static void Release(ColliderWrapper wrapper)
            {
                if (wrapper != null)
                {
                    if (!_pools.ContainsKey(wrapper.GetType()))
                    {
                        _pools.Add(wrapper.GetType(), new Queue<ColliderWrapper>());
                    }
                    _pools[wrapper.GetType()].Enqueue(wrapper);
                }
            }
            /// <summary>
            /// 创建一个对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static T Creat<T>() where T : ColliderWrapper,new()
            {
                if (_pools.ContainsKey(typeof(T)))
                {
                    if (_pools[typeof(T)].Count > 0)
                    {
                        return (T)_pools[typeof(T)].Dequeue();
                    }
                }
                return new T();
            }
            
        }
    }
}